/**
 * Created by gongxun on 17/2/13.
 */

var WhmjAnimLaiziLayer = GameCCSBase.extend({
    Panel_bg : null,
    Panel_light : null,
    Image_center : null,
    mLaiziCard : null,

    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/WhmjAnimLaizi.json");
        this.Image_center = mo.seekWidgetByName(this.ccsNode, "Image_center");
        this.Panel_bg = mo.seekWidgetByName(this.ccsNode, "Panel_bg");
        this.Panel_light = mo.seekWidgetByName(this.ccsNode, "Panel_light");

        this.mLaiziCard = new WhmjCard();
        mo.seekWidgetByName(this.ccsNode, "Panel_laizi").addChild(this.mLaiziCard);
        this.mLaiziCard.setVisible(false);

        this.stopAnimLaizi();
    },

    startAnimLaizi : function(laiziNum){
        if(!mo.whmjCardHelper.getCardNumIsBasicLaw(laiziNum)){
            return;
        }

        var that = this;
        this.mLaiziCard.setVisible(false);

        this.scheduleOnce(function(dt){
            //背景
            that.Panel_bg.setVisible(true);
            that.Panel_light.setVisible(true);
            that.Image_center.stopAllActions();
            that.Image_center.runAction(cc.repeatForever(
                cc.sequence(
                    cc.rotateBy(1.5, 120)
                )
            ));

            //赖子牌
            that.mLaiziCard.stopAllActions();
            that.mLaiziCard.setCardNum(laiziNum);
            that.mLaiziCard.setCardKind(0, 3);
            that.mLaiziCard.setScale(1);
            that.mLaiziCard.setVisible(true);
            var realSize = that.mLaiziCard.getBoundingBox();
            that.mLaiziCard.setPositionX(mo.gameSize.x / 2 - realSize.width / 2);
            that.mLaiziCard.setPositionY(mo.gameSize.y / 2 - realSize.height / 2);

            var toScale = mo.whmjCardHelper.dealScale;
            var cardSize = mo.whmjCardHelper.inhandCardSize;
            var toPos = cc.p(50 - toScale * cardSize.width / 2, mo.gameSize.y / 2 + 30 - toScale * cardSize.height / 2);
            that.mLaiziCard.runAction(cc.sequence(
                cc.delayTime(1),
                cc.callFunc(function() {
                    that.stopAnimLaizi();
                }),
                cc.spawn(
                    cc.moveTo(0.5, toPos),
                    cc.scaleTo(0.5, toScale)
                )
            ));

        }, 0.1);
    },

    setLaizi : function(laiziNum){
        this.mLaiziCard.stopAllActions();
        this.mLaiziCard.setCardNum(laiziNum);
        this.mLaiziCard.setCardKind(0, 3);
        this.mLaiziCard.setVisible(true);
        var realSize = this.mLaiziCard.getBoundingBox();

        var toScale = mo.whmjCardHelper.dealScale;
        var cardSize = mo.whmjCardHelper.inhandCardSize;
        var toPos = cc.p(50 - toScale * cardSize.width / 2, mo.gameSize.y / 2 + 30 - toScale * cardSize.height / 2);
        this.mLaiziCard.setScale(toScale);
        this.mLaiziCard.setPosition(toPos);
    },

    stopAnimLaizi : function(){
        this.Image_center.stopAllActions();
        this.Panel_bg.setVisible(false);
        this.Panel_light.setVisible(false);
    }


});